package com.ly.mpayment.rest.system;

import com.ly.mpayment.core.util.*;
import com.ly.mpayment.persist.entity.BlackList;
import com.ly.mpayment.persist.entity.Mobile;
import com.ly.mpayment.persist.service.blacklist.IBlackListService;
import com.ly.mpayment.persist.service.mobile.IMobileService;
import com.ly.mpayment.persist.service.user.IUserService;
import com.ly.mpayment.rest.AbsRest;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Component
@Path("system/blacklist/")
public class BlackListRest extends AbsRest{
	@Autowired
	IBlackListService blackListService;

	@Autowired
	IUserService userService;

	@Path("getBlacklistPage")
	@POST
	@SuppressWarnings("static-access")
	public String getBlacklistPage(String data,@Context HttpServletRequest request){
		String result = this.validationParameter(data, request, userService,"start","pageSize","phone");
		if(result == null){
			JSONObject json = JsonUtils.getJSONOBJCT().fromObject(data);
			Map<String, Object> parameters = MapUtil.getMap();
			if(!StringUtils.isBlank(json.getString("phone"))){
				parameters.put(":phone", "%".concat(json.getString("phone")));
			}
			return ResultMessage.getSucceed( JsonUtils.excludes(this.blackListService.findPageBlackList(parameters, json.getInt("start"), json.getInt("pageSize")), new String[]{"cityCode","postCode"}));
		}
		return result;
	}

	/**
	 * 添加黑名单
	 * @param data
	 * @param request
	 * @return
	 */
	@Path("updateBlacklist")
	@POST
	@SuppressWarnings("static-access")
	public String updateBlacklist(String data,@Context HttpServletRequest request){
		String result = this.validationParameter(data,  request, userService,"phone","note");
		if(result == null){
			JSONObject json = JSONObject.fromObject(data);
			if(!StringUtils.isBlank(json.getString("phone").trim())){
				if(json.getString("phone").contains(",")){
					String phs [] = json.getString("phone").split(",");
					for(int i = 0 ; i < phs.length; i++){
						JSONObject dataJ = new JSONObject();
						dataJ.put("note",json.getString("note"));
						dataJ.put("phone",phs[i]);
						addList(dataJ);
					}
					return ResultMessage.getSucceed(null);
				}else{
					return addList(json) ?  ResultMessage.getSucceed(null) :  ResultMessage.getFailt("");
				}

			}
			return ResultMessage.getFailt("");
		}
		return result;
	}

	private boolean addList(JSONObject json){
		BlackList mobile = this.blackListService.findPhone(json.getString("phone"));
		if(mobile != null && blackListService.updateBlackList(mobile)){
			return true;
		}else {
			mobile = new BlackList();
			//mobile.setBlackId(UUID.randomUUID().toString().replace("-",""));
			mobile.setNote(json.getString("note"));
			mobile.setPhone(json.getString("phone"));
			blackListService.saveBlackList(mobile);
			return true;
		}
	}

	@Path("deleteBlacklist")
	@POST
	@SuppressWarnings("static-access")
	public String deleteBlacklist(String data,@Context HttpServletRequest request){
		String result = this.validationParameter(data, request, userService, "blackId");
		if(result == null){
			JSONObject json = JSONObject.fromObject(data);
			if(!StringUtils.isBlank(json.getString("blackId").trim())){
				BlackList mobile = this.blackListService.findById(json.getString("blackId"));
				if(blackListService.deleteForm(mobile)){
					return ResultMessage.getSucceed(null);
				}
			}
			return ResultMessage.getFailt("");
		}
		return result;
	}


}
